// void, Obj me, Obj u

Unit this, tgt;
Building b;
Catapult c;
point pt;

this = me.AsUnit();
pt = u.posRH;
//tgt = u.AsUnit;
//This Script Is Modified For Attacking Buildings (without using BOEINGS)
//If we can capture the building - OK script returns, building captured.
//Else search for enemy unit - if found Attack Unit Until Dead
//then recall building and do the same again

//pr("unit::attack: attacking particular enemy");
b = u.AsBuilding(); // Remember the building

if (.IsValid) {// leave parry mode at new command
	.SetParryMode(false);
	
	if(.hero.IsValid){
		if(.hero.HasItem("irage_battle")){
			Sleep(rand(200)+50);
			.AddCommand(true, "engage");
			return;
		} else if(.hero.HasItem("irage_battle_stand")){
			Sleep(rand(200)+50);
			.AddCommand(true, "stand_position");
			return;
		}
	}
	
	if(.item_count > 0){
		Item item;
		
		item = .FindItem("Terrain bonus I");
		if(item.IsValid)
			.RemoveItemsOfType("Terrain bonus I");
		else {
			item = .FindItem("Terrain bonus II");
			if(item.IsValid)
				.RemoveItemsOfType("Terrain bonus II");
			else {
				item = .FindItem("Terrain bonus III");
				if(item.IsValid)
					.RemoveItemsOfType("Terrain bonus III");
				else {
					item = .FindItem("Terrain bonus IV");
					if(item.IsValid)
						.RemoveItemsOfType("Terrain bonus IV");
				}
			}
		}
	}
}

while(u.IsAlive){
	Sleep(rand(200)+100);
	//pr("Mele attack"); 
	if(!.IsEnemy(u)) break;

	tgt = u.AsUnit();
	c = u.AsCatapult();
	
       
	if(tgt.IsValid() || c.IsValid()){//Unit Logic
                	
		while(u.IsAlive() && .IsValidTarget(u)){
			
			if(!.IsVisible){
				.SetLastAttackTime();
				.SetVisible(true); 
			}
			//pr("unit::attack: going to enemy");
			if(.GotoAttack(u, 1500, true, 2500)){
				//pr("unit::attack: fighting");
				if(!u.IsMilitary){
					//if(!.IsHeirOf("Horse"))
						while(.Attack(u));
				} else {
					Sleep(rand(200)+100);
					.AddCommand(true, "advance", pt);
					.AddCommand(true, "engage_unit_type", tgt);
					return;
				}
			}
			//if (.Goto(u.posRH, 0, 2000, true, 0)) return;
		}
 
	} else {//Building Logic
	
		//pr("Building Logic");
		if(b.IsValid())
			if(.IsEnemy(b)){
				Sleep(rand(200)+100);
				.AddCommand(true, "capture", b);
				return;
			}
	}
}
